#include "PhysicsParticle.h"
#include "PhysicsCalculations.h"
//#include "DX11InputManager.h"
#include "ObjMeshManager.h"

PhysicsParticle::PhysicsParticle()
{
	m_position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_oldPosition = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_velocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_acceleration = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_force = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_mass = 1.0f;
	m_posCalc = EULER;
}

void PhysicsParticle::Draw()
{
	TheObjMeshManager::Instance()->Translate("crate_textured", m_position);
	TheObjMeshManager::Instance()->Scale("crate_textured", 0.5f, 0.5f, 0.5f);
	TheObjMeshManager::Instance()->Draw("crate_textured");
}

void PhysicsParticle::Update()
{
	static const D3DXVECTOR3 GRAVITY = D3DXVECTOR3(0.0f, -0.2f, 0.0f);
	
	m_acceleration = m_force * m_mass;
	if (m_mass > 0)
	{
		m_acceleration += GRAVITY;
	}

	//m_acceleration *= 0.9f;

	m_force = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	//if (TheDX11InputManager::Instance()->GetKeyPressed(DIK_SPACE))
	{
		if (m_posCalc == EULER)
		{
			m_posCalc = VERLET;
		}
		else if (m_posCalc == VERLET)
		{
			m_posCalc = EULER;
		}
	}

	if (m_mass > 0)
	{
		if (m_posCalc == EULER)
		{
			CalculatePositionE(m_position, m_oldPosition, m_velocity, m_acceleration);
		}
		else if (m_posCalc == VERLET)
		{
			CalculatePositionV(m_position, m_oldPosition, m_acceleration);
		}
	}
}

D3DXVECTOR3& PhysicsParticle::GetPos()
{
	return m_position;
}

void PhysicsParticle::AddForce(const D3DXVECTOR3& force)
{
	m_force += force;
}

void PhysicsParticle::SetMass(float mass)
{
	m_mass = mass;

	if (m_mass == 0.0f)
	{
		m_inverseMass = 1.0f / m_mass;
	}
	else
	{
		m_inverseMass = 0.0f;
	}
}